package com.example.seataAccount.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.seataAccount.common.enums.RspStatusEnum;
import com.example.seataAccount.common.response.ObjectResponse;
import com.example.seataAccount.entity.Account;
import com.example.seataAccount.mapper.AccountMapper;
import org.springframework.stereotype.Service;

@Service
public class AccountService {

    private final AccountMapper accountMapper;

    public AccountService(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    /**
     * 减金额
     * @param userId  用户ID
     * @param orderMoney 订单金额
     */
    public ObjectResponse<Object> decreaseAccount(String userId, Double orderMoney){
        ObjectResponse<Object> response = new ObjectResponse<>();
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Account account = accountMapper.selectOne(wrapper);
        account.setAmount(account.getAmount()-orderMoney);
        int result = accountMapper.updateById(account);
        if (result > 0){
            response.setStatus(RspStatusEnum.SUCCESS.getCode());
            response.setMessage(RspStatusEnum.SUCCESS.getMessage());
            return response;
        }
        response.setStatus(RspStatusEnum.FAIL.getCode());
        response.setMessage(RspStatusEnum.FAIL.getMessage());
        return response;
    }
}
